package com.zzh.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;


/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-08-21
 * @desc
 */
public class CustomImageView extends AppCompatImageView {

    //画笔 背景
    private Paint mPaint;
    // 画笔 文字
    private Paint mTextPaint;
    // 绘制的背景颜色
    private int bgColor;
    // 绘制的文本颜色
    private int textColor;
    // 绘制的文本大小
    private int textSize;
    // 绘制的圆角
    private int radius;
    // 展示的内容
    private String mTextValue;


    public CustomImageView(Context context) {
        this(context, null);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, 0, 0);
        bgColor = typeArray.getColor(R.styleable.CustomImageView_bgColor, Color.parseColor("#0000ff"));
        textColor = typeArray.getColor(R.styleable.CustomImageView_textColor, Color.parseColor("#00ff00"));
        textSize = typeArray.getDimensionPixelSize(R.styleable.CustomImageView_textSize, 24);
        radius = typeArray.getDimensionPixelSize(R.styleable.CustomImageView_radius, 5);
        mTextValue = typeArray.getString(R.styleable.CustomImageView_textValue);
        typeArray.recycle();

        mPaint.setColor(bgColor);
        mTextPaint.setColor(textColor);
        mTextPaint.setTextSize(textSize);
        setTextValue(mTextValue);
    }


    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        invalidate();
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        invalidate();
    }

    @Override
    public void setImageDrawable(@Nullable Drawable drawable) {
        super.setImageDrawable(drawable);
        invalidate();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        Drawable drawable = getDrawable();
        if (drawable != null && drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            if (bitmap != null) {
                return;
            }
        }
        // 绘制背景
        canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), radius, radius, mPaint);
        // 绘制文字
        canvas.drawText(mTextValue, getWidth() / 2 - (mTextWidth / 2), getHeight() / 2 + (mTextHeight / 2), mTextPaint);
    }

    private int mTextWidth = 0;
    private int mTextHeight = 0;

    public void setTextValue(String textValue) {
        String newTextValue = TextUtils.isEmpty(textValue) ? "" : textValue;
        if (TextUtils.equals(newTextValue, mTextValue)) {
            return;
        }
        this.mTextValue = newTextValue;
        Rect rect = new Rect();
        mTextPaint.getTextBounds(mTextValue, 0, mTextValue.length(), rect);
        mTextWidth = rect.width();
        mTextHeight = rect.height();
        invalidate();
    }
}
